package com.computergames.server.service.reconnect;

import com.computergames.server.message.ReconnectResponseMessage;
import com.computergames.server.service.Implement.GobangMessageHandlerFactory;
import com.computergames.server.service.Interface.GameMessageHandlerFactory;
import com.computergames.server.util.SpringUtil;
import io.netty.channel.ChannelHandlerContext;
import io.netty.util.AttributeKey;

import java.util.List;

public class GobangReconnect implements ReconnectStrategy{
    private static final AttributeKey<String> USER_ROOM_KEY = AttributeKey.valueOf("USER_ROOM");
    @Override
    public void reconnect(ChannelHandlerContext ctx) {
        GameMessageHandlerFactory gobangMessageHandlerFactory = SpringUtil.getBean(GobangMessageHandlerFactory.class);
        List<String> recordList = gobangMessageHandlerFactory.getGameStorage().getValuesForKey(ctx.channel().attr(USER_ROOM_KEY).get());
        String recordStr = String.join(";", recordList);
        ctx.channel().writeAndFlush(new ReconnectResponseMessage(true, recordStr));
    }
}
